<?php
/**
 * Created by mww
 * User: mww
 */

namespace app\admin\controller;
use think\Db;
use think\Request;

class Project extends Base
{
    /**
     * 首页
     */
    public function index(){
        if(request()->isAjax()){
            extract(input());
            $where = [
                'pro_del' => 1
            ];
            if(isset($jump_uid) && $jump_uid){
                $pu_pro_ids = Db::table('up_project_user')
                    ->where('pu_user_id',$jump_uid)
                    ->where('pu_status',2)
                    ->column('pu_pro_id');
                $where['pro_id'] = ['in',$pu_pro_ids];
            }
            if(session("company_id") > 0){
                $where['pro_c_id'] = session("company_id");
            }
            if(isset($user_name) && $user_name != ''){
                $where['pro_name|user_username|user_phone'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($pro_status) && $pro_status != ''){
                $where['pro_status'] = $pro_status;
            }
            //开始时间查询
            if(isset($start) && $start != "" && isset($end) && $end == "")
            {
                $where['pro_start_time'] = ['>=',$start];
            }
            if(isset($end) && $end != "" && isset($start) && $start == "")
            {
                $where['pro_start_time'] = ['<=',$end];
            }
            if(isset($start) && $start != "" && isset($end) && $end != "")
            {
                $where['pro_start_time'] = ['between',[$start,$end]];
            }
            //结束时间查询
//            if(isset($loginstart) && $loginstart != "" && isset($loginend) && $loginend == "")
//            {
//                $where['pro_end_time'] = ['>=',$loginstart];
//            }
//            if(isset($loginend) && $loginend != "" && isset($loginstart) && $loginstart == "")
//            {
//                $where['pro_end_time'] = ['<=',$loginend];
//            }
//            if(isset($loginstart) && $loginstart != "" && isset($loginend) && $loginend != "")
//            {
//                $where['pro_end_time'] = ['between',[$loginstart,$loginend]];
//            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $field = 'pro.*,c_name,cp_name,user_username,user_image,user_phone';
            $count = Db::table('up_project')->alias('pro')
                ->join('up_company c','pro.pro_c_id = c.c_id','left')
                ->join('up_company_product cp','pro.pro_cp_id = cp.cp_id','left')
                ->join('up_u_user u','pro.pro_user_id = u.user_id','left')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_project')->alias('pro')
                ->join('up_company c','pro.pro_c_id = c.c_id','left')
                ->join('up_company_product cp','pro.pro_cp_id = cp.cp_id','left')
                ->join('up_u_user u','pro.pro_user_id = u.user_id','left')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('pro_status asc,pro_update desc')
                ->field($field)
                ->select();
            if($lists){
                foreach ($lists as $k=>$v){
                    $lists[$k]['datas'] = Db::table('up_project_data')
                        ->where('prod_pro_id',$v['pro_id'])
                        ->count();
                }
            }
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $jump_uid = input('jump_uid');
        if(!isset($jump_uid) || !$jump_uid){
            $jump_uid = '';
        }
        $this->assign('jump_uid',$jump_uid);
        return $this->fetch();
    }
    /**
     * 删除
     * @return \think\response\Json
     */
    public function proDel(){
        $pro_id = input('id');
        $res = Db::table('up_project')->where('pro_id',$pro_id)->setField('pro_del',2);
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    //获取业务员和产品
    public function getUsers()
    {
        $cid = input('param.cid');
        $product = Db::table('up_company_product')
            ->where('cp_c_id',$cid)
            ->where('cp_status',1)
            ->field('cp_id,cp_name')
            ->order('cp_update desc')
            ->select();
        $data = Db::table('up_u_user')
            ->where('user_hospital',$cid)
            ->where('user_status',1)
            ->where('user_verify',2)
            ->field('user_id,user_username,user_phone')
            ->order('user_auth desc')
            ->select();
        return json(['code'=>200,'msg'=>$data,'product'=>$product]);
    }
    /**
     * 添加
     * @return mixed|\think\response\Json
     */
    public function addProject(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $insert = [
                'pro_name' => $pro_name,
                'pro_user_id' => $pro_user_id,
                'pro_c_id' => $pro_c_id,
                'pro_cp_id' => $pro_cp_id,
                'pro_start_time' => $pro_start_time,
                'pro_end_time' => $pro_end_time,
                'pro_time' => $today,
                'pro_update' => $today,
            ];
            if($pro_start_time >= $pro_end_time){
                return json(['code' => 100, 'msg' => '开始时间不能大于结束时间']);
            }
            if($pro_start_time > $today){
                $insert['pro_status'] = 1;
            }
            if ($pro_end_time < $today){
                $insert['pro_status'] = 3;
            }
            if ($pro_start_time <= $today && $pro_end_time >= $today){
                $insert['pro_status'] = 2;
            }
            $res = Db::table('up_project')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        $where = [
            'c_verify' => 2,
            'c_status' => 1
        ];
        if(session("company_id") > 0){
            $where['c_id'] = session("company_id");
        }
        $coms = Db::table('up_company')
            ->where($where)
            ->field('c_id,c_name')
            ->select();
        $this->assign('coms',$coms);
        return $this->fetch('project/add');
    }
    /**
     * 编辑
     * @return mixed|\think\response\Json
     */
    public function proEdit(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'pro_name' => $pro_name,
                'pro_user_id' => $pro_user_id,
                'pro_c_id' => $pro_c_id,
                'pro_cp_id' => $pro_cp_id,
                'pro_start_time' => $pro_start_time,
                'pro_end_time' => $pro_end_time,
                'pro_update' => $today,
            ];
            if($pro_start_time >= $pro_end_time){
                return json(['code' => 100, 'msg' => '开始时间不能大于结束时间']);
            }
            if($pro_start_time > $today){
                $update['pro_status'] = 1;
            }
            if ($pro_end_time < $today){
                $update['pro_status'] = 3;
            }
            if ($pro_start_time <= $today && $pro_end_time >= $today){
                $update['pro_status'] = 2;
            }
            $res = Db::table('up_project')->where('pro_id',$pro_id)->update($update);
            if($res){
                $this->editLikes(2,$pro_id);
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $pro_id = input('pro_id');
        $info = Db::table('up_project')
            ->where('pro_id',$pro_id)
            ->find();
        //
        $where = [
            'c_verify' => 2,
            'c_status' => 1
        ];
        if(session("company_id") > 0){
            $where['c_id'] = session("company_id");
        }
        $coms = Db::table('up_company')
            ->where($where)
            ->field('c_id,c_name')
            ->select();
        $product = Db::table('up_company_product')
            ->where('cp_c_id',$info['pro_c_id'])
            ->where('cp_status',1)
            ->field('cp_id,cp_name')
            ->order('cp_update desc')
            ->select();
        $users = Db::table('up_u_user')
            ->where('user_hospital',$info['pro_c_id'])
            ->where('user_status',1)
            ->where('user_verify',2)
            ->field('user_id,user_username,user_phone')
            ->order('user_auth desc')
            ->select();
        $this->assign([
            'info' => $info,
            'coms' => $coms,
            'product' => $product,
            'users' => $users
        ]);
        return $this->fetch('project/edit');
    }
    //-------------------------------------资料-------------------------------------------
    /**
     * 查看资料
     * @return mixed|\think\response\Json
     */
    public function seeDatas(){
        if(request()->isAjax()){
            extract(input());
            $where['prod_pro_id'] = session('pro_id');
            if(isset($user_name) && $user_name != ''){
                $where['prod_name'] = ['like', '%' . $user_name . '%'];
            }
            if(isset($cpd_type) && $cpd_type != ''){
                $where['prod_type'] = $cpd_type;
            }
            $NowPage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_project_data')
                ->where($where)
                ->count();// 获取总条数
            $lists = Db::table('up_project_data')
                ->where($where)
                ->page($NowPage,$limits)
                ->order('prod_top desc,prod_sort desc,prod_update desc')
                ->select();
            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        $pro_id = input('pro_id');
        session('pro_id', $pro_id);
        return $this->fetch('project/datas');
    }
    /**
     * 设置排序值
     * @return mixed|\think\response\Json
     */
    public function updateSort(){
        $id = input('id');
        $param = [
            'prod_sort'=>input('value'),
            'prod_update'=>date('Y-m-d H:i:s'),
        ];
        $res = Db::table('up_project_data')->where('prod_id',$id)->update($param);
        if($res){
            return json(['code' => 200, 'msg' => '修改成功']);
        }else{
            return json(['code' => 100, 'msg' =>' 修改失败']);
        }
    }
    /**
     * 改置顶状态
     * @author
     */
    public function changeTop()
    {
        extract(input());
        $res = Db::table('up_project_data')
            ->where('prod_id',$id)
            ->update([
                'prod_update' => date('Y-m-d H:i:s'),
                'prod_top' => $num
            ]);
        if($res){
            return json(['code' => 200, 'msg' => '成功']);
        }else{
            return json(['code' => 100, 'msg' => '失败']);
        }
    }

    /**
     * 删除资料
     * @return \think\response\Json
     */
    public function dataDel(){
        $ct_id = input('id');
        $res = Db::table('up_project_data')->where('prod_id',$ct_id)->delete();
        if($res){
            return json(['code' => 200, 'msg' => '删除成功']);
        }else{
            return json(['code' => 100, 'msg' => '删除失败']);
        }
    }
    /**
     * 添加资料
     * @return mixed|\think\response\Json
     */
    public function addData(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $insert = [
                'prod_pro_id' => session('pro_id'),
                'prod_name' => $prod_name,
                'prod_type' => $prod_type,
                'prod_thumb' => $prod_thumb,
                'prod_sort' => $prod_sort,
                'prod_time' => $today,
                'prod_update' => $today,
            ];
            //内容
            if($prod_type == 1){
                if(!$prod_content1){
                    return json(['code' => 100, 'msg' => '图片不能为空']);
                }
                $insert['prod_content'] = $prod_content1;
            }elseif ($prod_type == 2){
                if(!$prod_content2){
                    return json(['code' => 100, 'msg' => '文档不能为空']);
                }
                $insert['prod_content'] = $prod_content2;
            }else{
                if(!$prod_content3){
                    return json(['code' => 100, 'msg' => '视频不能为空']);
                }
                $insert['prod_content'] = $prod_content3;
            }
            //缩略图
            if(!$prod_thumb){
                if($prod_type == 1){
                    $thumb_arr = explode(',',$prod_content1);
                    $insert['prod_thumb'] = $thumb_arr[0];
                }elseif ($prod_type == 2){
                    $insert['prod_thumb'] = config('pdfurl')['url'];
                }else{
                    $insert['prod_thumb'] = $prod_content3 . '?ci-process=snapshot&time=1';
                }
            }
            $res = Db::table('up_project_data')->insert($insert);
            if($res){
                return json(['code' => 200, 'msg' => '添加成功']);
            }else{
                return json(['code' => 100, 'msg' => '添加失败']);
            }
        }
        return $this->fetch('project/add_data');
    }
    /**
     * 编辑资料
     * @return mixed|\think\response\Json
     */
    public function editData(){
        if(Request::instance()->isPost()){
            extract(input());
            $today = date('Y-m-d H:i:s');
            $update = [
                'prod_name' => $prod_name,
                'prod_type' => $prod_type,
                'prod_thumb' => $prod_thumb,
                'prod_sort' => $prod_sort,
                'prod_update' => $today,
            ];
            //内容
            if($prod_type == 1){
                if(!$prod_content1){
                    return json(['code' => 100, 'msg' => '图片不能为空']);
                }
                $update['prod_content'] = $prod_content1;
            }elseif ($prod_type == 2){
                if(!$prod_content2){
                    return json(['code' => 100, 'msg' => '文档不能为空']);
                }
                $update['prod_content'] = $prod_content2;
            }else{
                if(!$prod_content3){
                    return json(['code' => 100, 'msg' => '视频不能为空']);
                }
                $update['prod_content'] = $prod_content3;
            }
            //缩略图
            if(!$prod_thumb){
                if($prod_type == 1){
                    $thumb_arr = explode(',',$prod_content1);
                    $update['prod_thumb'] = $thumb_arr[0];
                }elseif ($prod_type == 2){
                    $update['prod_thumb'] = config('pdfurl')['url'];
                }else{
                    $update['prod_thumb'] = $prod_content3 . '?ci-process=snapshot&time=1';
                }
            }
            $res = Db::table('up_project_data')->where('prod_id', $prod_id)->update($update);
            if($res){
                return json(['code' => 200, 'msg' => '编辑成功']);
            }else{
                return json(['code' => 100, 'msg' => '编辑失败']);
            }
        }
        $prod_id = input('prod_id');
        $info = Db::table('up_project_data')
            ->where('prod_id',$prod_id)
            ->find();
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch('project/edit_data');
    }
    /**
     * 资料详情
     * @return mixed|\think\response\Json
     */
    public function dataInfo(){
        $prod_id = input('prod_id');
        $info = Db::table('up_project_data')
            ->where('prod_id',$prod_id)
            ->find();
        //------------------
        if($info['prod_type'] == 1){
            if($info['prod_content']){
                $info['prod_content'] = explode(',',$info['prod_content']);
            }else{
                $info['prod_content'] = [];
            }
        }
        $this->assign([
            'info' => $info
        ]);
        return $this->fetch('project/data_info');
    }
}